import {query as queryUsers, queryCurrent} from '@/services/user';
import {setAuthority} from "@/utils/authority";

const UserModel = {
    namespace: 'user',
    state: {
        currentUser: {},
    },
    effects: {
        * fetch(_, {call, put}) {
            const response = yield call(queryUsers);
            yield put({
                type: 'save',
                payload: response,
            });
        },

        * fetchCurrent(_, {call, put}) {
            const utypeId = sessionStorage.getItem('UtypeId');
            const response = yield call(queryCurrent, utypeId);
            if (response) {
                response.avatar = 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png';
                sessionStorage.removeItem('MYRCIB_USER_TOKEN');
                sessionStorage.setItem('MYRCIB_USER_TOKEN', response.MYRCIB_USER_TOKEN);
            }
            yield put({
                type: 'saveCurrentUser',
                payload: response,
            });
        }
    },
    reducers: {
        saveCurrentUser(state, action) {
            return {...state, currentUser: action.payload || {}};
        },

        changeNotifyCount(
            state = {
                currentUser: {},
            },
            action,
        ) {
            return {
                ...state,
                currentUser: {
                    ...state.currentUser,
                    notifyCount: action.payload.totalCount,
                    unreadCount: action.payload.unreadCount,
                }
            };
        }
    }
};
export default UserModel;
